//
//  MCCommunityCell.swift
//  StoreManage
//
//  Created by MC on 2018/6/22.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit
typealias CommunityClosure = (Int,MCCommunityModel,UIButton) -> Void


class MCCommunityCell: UITableViewCell {

    public var closure : CommunityClosure?
    
    var model = MCCommunityModel() {
        didSet {
            nameLabel.text = model.communityName
            addressLabel.text = model.province + model.city + model.area + model.address
            addressButton.isSelected = model.isDefault.boolValue
        }
    }
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
  
        self.addSubview(bgView)
        bgView.snp.remakeConstraints { (make) ->Void in
            make.edges.equalTo(self).inset(UIEdgeInsetsMake(0, 0, 7, 0))
        }
    
        bgView.addSubview(lineView)
        lineView.snp.remakeConstraints { (make) ->Void in
            make.left.right.equalTo(bgView)
            make.height.equalTo(1)
            make.bottom.equalTo(bgView).offset(-40)
        }
        
        bgView.addSubview(addressButton)
        addressButton.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(15)
            make.top.equalTo(lineView.snp.bottom)
            make.bottom.equalTo(bgView)
        }
        
        bgView.addSubview(editButton)
        editButton.snp.remakeConstraints { (make) ->Void in
            make.right.equalTo(-15)
            make.top.equalTo(lineView.snp.bottom)
            make.bottom.equalTo(bgView)
        }
        
//        bgView.addSubview(editButton)
//        editButton.snp.remakeConstraints { (make) ->Void in
//            make.right.equalTo(deleteButton.snp.left).offset(-20)
//            make.top.equalTo(lineView.snp.bottom)
//            make.bottom.equalTo(bgView)
//        }

        bgView.addSubview(nameLabel)
        nameLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.top.equalTo(10)
            make.height.equalTo(20)
        }
        
        bgView.addSubview(addressLabel)
        addressLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.top.equalTo(nameLabel.snp.bottom).offset(5)
            make.bottom.equalTo(lineView.snp.top).offset(-5)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    @objc func buttonClicked(sender:UIButton) {
        
        if MCUserinfo_getLevel() < MCRole_manager {
            
            MCHUD_text(message: "只有店长才可编辑", view: self, delay: 1.2)
            return;
        }
        
        closure?(sender.tag-1000,model,sender)
    }
    
    lazy var bgView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        return view
    }()
    
    lazy var nameLabel: MCLabel = {
        let label = MCLabel()
        label.font = UIFont.boldSystemFont(ofSize: 15)
        return label
    }()
    
    lazy var addressLabel: MCLabel = {
        let label = MCLabel()
        label.font = MCFont(14)
        label.numberOfLines = 0
        return label
    }()
    
    lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.MCGray_light
        return view
    }()
    
    lazy var addressButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.titleLabel?.font = MCFont(14)
        button.setTitleColor(UIColor.MCGray, for: .normal)
        button.setTitle(" 默认地址", for:.normal)
        button.setImage(#imageLiteral(resourceName: "GoodsManage_notSelected"), for: .normal)
        button.setImage(#imageLiteral(resourceName: "GoodsManage_selected"), for: .selected)
        button.tag = 1000
        button.addTarget(self, action: #selector(buttonClicked), for: .touchUpInside)
        return button
    }()
    
    lazy var editButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.titleLabel?.font = MCFont(14)
        button.setTitleColor(UIColor.MCGray, for: .normal)
        button.setTitle(" 编辑", for:.normal)
        button.setImage(#imageLiteral(resourceName: "Mine_edit").MCImageWithTintColor(color: UIColor.MCGray_middle), for: .normal)
        button.tag = 1001
        button.addTarget(self, action: #selector(buttonClicked), for: .touchUpInside)
        return button
    }()
    
    lazy var deleteButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.titleLabel?.font = MCFont(14)
        button.setTitleColor(UIColor.MCGray, for: .normal)
        button.setTitle(" 删除", for:.normal)
        button.setImage(#imageLiteral(resourceName: "Reuse_delete").MCImageWithTintColor(color: UIColor.MCGray_middle), for: .normal)
        button.tag = 1002
        button.addTarget(self, action: #selector(buttonClicked), for: .touchUpInside)
        return button
    }()

}
